package edu.spec.xhd.intent;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import edu.spec.xhd.R;


public class ActivityA extends AppCompatActivity {

    private Button buttonIntent;
    private Button buttonIntentC;
    private Button buttonIntentDail;
    private Button getButtonIntentView;
    private TextView textViewDataBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        initViews();
        initListeners();
    }

    private void initViews(){
        buttonIntent = findViewById(R.id.btn_intent);
        buttonIntentC = findViewById(R.id.btn_intent_c);
        buttonIntentDail = findViewById(R.id.btn_intent_dail);
        getButtonIntentView = findViewById(R.id.btn_intent_view);
        textViewDataBack = findViewById(R.id.tv_data_back);
    }

    private void initListeners(){
        buttonIntent.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String str = "SOS";
                Intent i = new Intent(ActivityA.this, ActivityB.class);
                i.putExtra("data", str);
                startActivityForResult(i, 1);//1是requestCode
            }
        });
        buttonIntentC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("edu.spec.xhd.activity.C");
                startActivity(intent);
            }
        });
        buttonIntentDail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
            }
        });
        getButtonIntentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1 && resultCode==RESULT_OK){
            String str = data.getStringExtra("data_return");
            textViewDataBack.setText(str);
        }
    }
}
